
const mysql = require('mysql');
const config = require('../config')

const conn = mysql.createConnection(config.dbconnection);

conn.connect(e => {
    if (e) console.log("Database: ", e)
    else console.log("Database connected.")
});

setInterval(function () {
    // Heartbeat signal
    conn.query('SELECT 1');
}, 5000);

async function query(sql, args) {
    // 改为promise风格
    return new Promise(function (resolve, reject) { 
        conn.query(sql, args, (err, result) => {
            if(err) reject(err)
            else resolve(result)
        })
    })
}

exports.query = query;
